package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.MyHomeActivityV2;
import com.runen.infinitemovement.model.entity.CommentDetailListBean;
import com.runen.infinitemovement.model.entity.ReplyCommentListBean;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

public class ContentsAdapter extends BaseQuickAdapter<CommentDetailListBean, BaseViewHolder> implements LoadMoreModule {
    private Context context;

    public ContentsAdapter(Context context) {
        super(R.layout.item_comment);
        this.context = context;
    }

    private OnItemCommentClick onItemCommentClick;

    public OnItemCommentClick getOnItemCommentClick() {
        return onItemCommentClick;
    }

    public void setOnItemCommentClick(OnItemCommentClick onItemCommentClick) {
        this.onItemCommentClick = onItemCommentClick;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, CommentDetailListBean item) {
        RoundedCornersImage rci_user_header = helper.getView(R.id.rci_user_header);
        GlideUtils.loadImage(context, item.headImgUrl + "?x-oss-process=image/resize,m_fill,w_144,h_144", rci_user_header);
        TextView tv_content = helper.getView(R.id.tv_content);
        TextView tv_userName = helper.getView(R.id.tv_userName);
        TextView tv_comment_time = helper.getView(R.id.tv_comment_time);
        RecyclerView rv_msg = helper.getView(R.id.rv_msg);
        tv_userName.setText(item.nickname);
        tv_comment_time.setText(TimeUtil.dealDateFormat(item.createTime));
        tv_content.setText(item.content);

        rci_user_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyHomeActivityV2.class);
                intent.putExtra("userId", item.reviewerId);
                context.startActivity(intent);
            }
        });

        tv_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemCommentClick) {
                    onItemCommentClick.onItemCommentReply(item, item.commentId);
                }
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rv_msg.setLayoutManager(layoutManager);
        CommentMessageAdapter commentMessageAdapter = new CommentMessageAdapter(context);
        rv_msg.setAdapter(commentMessageAdapter);
        List<ReplyCommentListBean> listBeans = new ArrayList<>();
        if (item.secComments != null && item.secComments.size() > 0) {
            for (int i = 0; i < item.secComments.size(); i++) {
                listBeans.add(item.secComments.get(i));
            }
            if (item.secCommentsNumber >= 2) {
                ReplyCommentListBean replyCommentListBean = new ReplyCommentListBean();
                String check = "查看全部 %s 条回复";
                replyCommentListBean.reviewerName = "CHECK_ALL";
                replyCommentListBean.content = String.format(check, item.secCommentsNumber);
                listBeans.add(replyCommentListBean);
            }
        }
        commentMessageAdapter.addData(listBeans);
        commentMessageAdapter.setOnSecondItemClick(new CommentMessageAdapter.OnSecondItemClick() {
            @Override
            public void onItemClick(ReplyCommentListBean secondItem) {
                if (onItemCommentClick != null) {
                    onItemCommentClick.onItemSecondReply(secondItem, item.commentId);
                }
            }

            @Override
            public void onMoreClick() {
                if (null != onItemCommentClick) {
                    onItemCommentClick.onItemMore(item, item.commentId);
                }
            }
        });
    }

    public interface OnItemCommentClick {
        /**
         * item中的一条信息点赞
         *
         * @param item
         */
        void onItemPraise(CommentDetailListBean item);

        /**
         * item中的一条信息进行回复
         *
         * @param item
         */
        void onItemCommentReply(CommentDetailListBean item, String topCommentId);

        /**
         * item中二级进行回复
         *
         * @param item
         */
        void onItemSecondReply(ReplyCommentListBean item, String topCommentId);

        /**
         * 跳到更多
         *
         * @param item
         * @param topCommentId
         */
        void onItemMore(CommentDetailListBean item, String topCommentId);
    }
}
